Skip to main content
Version: 2.14.4 - 2.14.0

RS485, Modbus & NVT

This category contains functions specific to RS-485 or MODBUS devices.

api.rs485Send(msg)

Sends message to RS-485 bus.

tip

Ensure RS-485 is turned on using api.rs485State(1) first.

api.rs485Setup(arg1, arg2, ...)

Configures the RS-485 communication interface.

api.rs485State(state)

Controls the RS-485 circuitry.

warning

This function must be called before api.rs485Send() or api.rs485Receive().

api.rs485Receive(timeout)

Waits timeout milliseconds for data reception from RS-485 bus.

tip

Ensure RS-485 is turned on using api.rs485State(1) first.

info

After the first character is received, the inter-character (i.e., inter-byte) timeout delay is set to 10 ms by default. This can be modified by providing a second optional argument to the function.

api.modbusCrc(msg)

Calculates MODBUS request checksum.

api.modbusRTU(address, registerAddr, functionCode, typeSpec, registerCount, timeout, retry, interByteTimeout)

Communicates with a MODBUS device over a serial connection using the RTU (Remote Terminal Unit) protocol.

info

It can be used to read or write one or more registers of various data types. The function takes several parameters, including the MODBUS device address, the starting register address, the function code, the type specification of the register(s), the number of registers to read or write, and several timeout and retry parameters.

The typeSpec parameter is a Lua string that specifies the data type of the register(s) being read or written. It uses the Lua Packing and Unpacking syntax to specify the type and formatting of the data. For example, to read a single 16-bit unsigned integer, the typeSpec parameter would be "H".

The function returns the value(s) read from or written to the MODBUS register(s), according to the specified typeSpec. For example, if typeSpec is "H" and registerCount is 2, the function would return two 16-bit unsigned integers.

If the function fails to communicate with the MODBUS device, it will retry the operation according to the retry parameter. If the operation still fails after the specified number of retries, the function will return nil.

api.nvtProcess(buf)

Processes NVT message and either sets baudrate, datasize, parity or stop size for MBUS or MODBUS.

api.nvtEncode(msg)

Encodes message to NVT format.